﻿#include "menudao.h"
#include <QSqlError>
#include <QDebug>

//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    menudao.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-08  10:17
*/
#include <menu.h>

MenuDao::MenuDao(QSqlDatabase sqlDatabase)
{
    db = sqlDatabase;
}

MenuDao::~MenuDao()
{

}

bool MenuDao::getListByParentId(QList<Menu> &list, uint64_t parentId)
{
    bool res = false;
    Menu menuTmp;
//    menuTmp.setParentId(parentId);
    QSqlQuery sql_query(db);
//    QString sqlStr = menuTmp.generateSelectSql();
    QString sqlStr = QString("%1 where parentId = %2").arg(menuTmp.generateSelectSql()).arg(QString::number(parentId,'f',0));

    if (sql_query.exec(sqlStr))
    {
        res = true;
        while (sql_query.next()){
            menuTmp.clear();
            menuTmp.fromSqlQuery(sql_query);
            list.append(menuTmp);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::getList(QList<Menu> &list)
{
    bool res = false;
    Menu menuTmp;
    QSqlQuery sql_query(db);
    QString sqlStr = menuTmp.generateSelectSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
        while (sql_query.next()){
            menuTmp.clear();
            menuTmp.fromSqlQuery(sql_query);
            list.append(menuTmp);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::getListByPageAndParam(QList<Menu> &list, const uint64_t &page, const uint64_t &pageSize)
{
    bool res = false;
    Menu menuTmp;
    QSqlQuery sql_query(db);
    uint64_t offset = (page - 1) * pageSize;
    QString sqlStr = QString("%1 LIMIT :limitVal OFFSET :offsetVal").arg(menuTmp.generateSelectSql());
    sql_query.prepare(sqlStr);
    sql_query.bindValue(":limitVal", pageSize);
    sql_query.bindValue(":offsetVal", offset);
    if (sql_query.exec())
    {
        res = true;
        while (sql_query.next())
        {
            menuTmp.clear();
            menuTmp.fromSqlQuery(sql_query);
            list.append(menuTmp);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::getTotalByParam(uint64_t &total)
{
    bool res = false;
    Menu menuTmp;
    QSqlQuery sql_query(db);
    QString sqlStr = QString("SELECT COUNT(*) FROM %1").arg(menuTmp.getTableName());
    if (sql_query.exec(sqlStr))
    {
        if (sql_query.next())
        {
            res = true;
            total = sql_query.value(0).toULongLong();
        }
        else
        {
            qCritical() << __FUNCTION__ << "not find COUNT!" << sqlStr;
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::getOne(Menu &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateSelectSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
        if(sql_query.next()){
            object.fromSqlQuery(sql_query);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::saveOne(Menu &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateInsertSql();
    if(object.getId() != 0){
        sqlStr = object.generateUpdateSql();
    }
    if (sql_query.exec(sqlStr))
    {
        res = true;
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool MenuDao::deleteOne(Menu &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateDeleteSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

